
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Device_Family extends JFrame{
    public Device_Family(String familyID){

        SQLOperation sql=new SQLOperation();




        String[] dataBelongs={"Lamps","WindowsDoors","Humidity","Temperature"};
        String[][] dataInfo={{"isOn","timeStamp"},
                {"isOpen","timeStamp"},
                {"hygro","timeStamp"},
                {"tempNum","timeStamp"}};


        String[] familyAttri={"idDevice"};


        Object[][] deviceData=sql.search("Device",familyAttri,"Family_familyID",familyID);



        if(deviceData.length!=0) {

            setTitle("Information");
            Container container = getContentPane();
            setLayout(new BorderLayout());

            JPanel titlePanel = new JPanel();

            JLabel title = new JLabel("Device Data of Family'" + familyID + "'");
            title.setFont(new Font("", Font.BOLD, 20));

            titlePanel.add(title);
            add(titlePanel,BorderLayout.NORTH);

            JComboBox selectDevice=new JComboBox();
            selectDevice.addItem("-------------");
            for(int i=0;i<deviceData.length;i++){
                selectDevice.addItem(deviceData[i][0]);
            }

            JPanel centerPanel=new JPanel();
            centerPanel.add(selectDevice);
            add(centerPanel,BorderLayout.CENTER);

            JButton search=new JButton("Search");
            search.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Object[][] detailData;
                    int num=-1;
                    for(int i=0;i<4;i++){
                        detailData=sql.search(dataBelongs[i],dataInfo[i],
                                "Device_idDevice",selectDevice.getSelectedItem());
                        if(detailData.length!=0){
                            num=i;
                            break;
                        }
                    }

                    if(selectDevice.getSelectedIndex()==0) {


                        JOptionPane.showMessageDialog
                                (null, "Please select a device!",
                                        "Empty select", JOptionPane.ERROR_MESSAGE);
                    }
                    else if(num==-1){


                        JOptionPane.showMessageDialog
                                (null, "Empty Data!",
                                        "Empty data", JOptionPane.ERROR_MESSAGE);
                    }

                    else{


                            new FactoryEquipList(num,(String)selectDevice.getSelectedItem());


                    }

                }
            });

            JButton back = new JButton("Back");
            back.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
            JPanel buttons=new JPanel();
            buttons.add(back);
            buttons.add(search);
            add(buttons,BorderLayout.SOUTH);


            setSize(400,200);
            setResizable(false);
            setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            setVisible(true);



        }
        else{
            JOptionPane.showMessageDialog
                    (null, "Invalid familyID!","Empty select",JOptionPane.ERROR_MESSAGE);
            setVisible(false);


        }




    }
}
